package com.api.devices.test;

import java.io.*;

public class LargeHexFileReader {
    private static final int BUFFER_SIZE = 8192; // 8KB缓冲区

    private static String filePath = "D:\\Project\\demo\\20250515210200.dat";
    public static void main(String[] args) {

        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath))) {
//            try{
//            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
            // 跳过第一行（如果有文本头）
            skipFirstLine(bis);

            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead;
            StringBuilder hexBuilder = new StringBuilder();

            while ((bytesRead = bis.read(buffer)) != -1) {
                for (int i = 0; i < bytesRead; i++) {
                    // 将每个字节转换为HEX字符串
                    hexBuilder.append(String.format("%02X", buffer[i]));

                    // 可以根据需要处理积累的HEX数据
                    if (hexBuilder.length() >= 32) { // 每32个字符处理一次
                        processHexChunk(hexBuilder.toString());
                        hexBuilder.setLength(0);
                    }
                }
            }

            // 处理剩余数据
            if (hexBuilder.length() > 0) {
                processHexChunk(hexBuilder.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void skipFirstLine(BufferedInputStream bis) throws IOException {
        int b;
        while ((b = bis.read()) != -1) {
            if (b == '\n') {
                break;
            }
        }
    }

    private static void processHexChunk(String hexChunk) {
        // 处理HEX数据块
        System.out.println("Processing HEX chunk: " + hexChunk);
    }
}